<?php
/**
 * Created by PhpStorm.
 * User: kai.yang
 * Date: 2019-01-01
 * Time: 14:16
 */

namespace User\Controller;


use Home\Controller\BaseController;
use mysql_xdevapi\Exception;
use Think\Log;

class AdminController extends BaseController
{



    public function query(){
        $nowTime=date('Y-m-d H:m:s',time());
        //通过I函数获取get获取post参数，get.表示只获取get参数，post表示只获取post参数，不写代表通用,后面的'kai'代表默认值
        $name = I('get.loginName', '');
        $mail = I('get.mail');
        $sex= I('get.sex');
        $startTime = I('get.startTime', '2015-01-01 00:00:00');
        $endTime= I('get.endTime', $nowTime);
        $model=M("users ");
        $map=array();
        $map["CREATE_TIME"]=array('egt',$startTime);
//        $map["CREATE_TIME"]=array('elt',$endTime);


        //判断获取到name参数是否为空，为空时，不传入此参数
        if($name){
            $map['name']=array('like',"%".$name."%");
        }

        if($mail){
            $map['mail']=array('like',"%".$mail."%");
        }

        if($sex){
            $map['sex']=$sex;
        }

        $count = $model->where($map)->count();
        $p = parent::getpage($count,C('PAGE_SIZE'));

        //users表和password表关联查询，根据名称和时间获取数据，查出最多2条，按时间倒序
        $result=$model->field("id,name,deploy_name,sex,CREATE_TIME,mail,status")->where($map)->order('id desc ')
            ->limit($p->firstRow, $p->listRows)->select();
        $this->assign("list",$result);
        $this->assign('page', $p->show()); // 赋值分页输出
        $this->assign('name',$name);
        $this->assign('mail',$mail);
        $this->assign('sex',$sex);
        $this->assign('endTime',$endTime);
        $this->assign('startTime',$startTime);

        $this->display(':users');

    }

    public function  addUser(){
        $this->display(':addUser');

    }



    public function doAddUser(){
        $loginNameArray = I('Post.loginNameArray');
        $mailArray=I('Post.mailArray');
        $deployNameArray=I('Post.deployNameArray');
        $sexArray=I('Post.sexArray');
        $passwordArray=I('Post.passwordArray');
        $model=M();
        $model->startTrans();  // 开启事务


        $addFlag=true;
        try {
            if (!empty($loginNameArray)) {
                for ($i = 0; $i < count($loginNameArray); $i++) {
                    $where = array();
                    $where['name'] = $loginNameArray[$i];
                    $id = $model->table('users')->where($where)->select();
                    if (!empty($id)) {
                        throw new \Exception("新增失败,用户" . $loginNameArray[$i] . "已存在！");
                    }
                    $user['name'] = $loginNameArray[$i];
                    $user['mail'] = $mailArray[$i];
                    $user['deploy_name'] = $deployNameArray[$i];
                    if($sexArray[$i]=='男'){
                        $user['sex'] = 1;
                    }else if($sexArray[$i]=='女'){
                        $user['sex'] = 2;
                    }else{
                        $user['sex'] = 0;
                    }

                    $user['level'] = 1;
                    $user['p_id'] = 0;
                    $user['status'] = "ACTIVE";
                    $newId = $model->table('users')->add($user);
                    $passwordModel['user_id']=$newId;
                    $passwordModel['pass']=$passwordArray[$i];
                    $passwordModel['pass_type']="LOGIN";
                    $model->table('password')->add($passwordModel);

                }

            }
        } catch (\Exception $e) {
            $addFlag=false;
        }

        if(!$addFlag){
            $result = array('message' => $e->getMessage(),
                            'code'=>0);
            $model->rollback();
        }else{
            $model->commit();
            $result = array('message' => '新增成功',
                'code'=>1);
        }
        $this->ajaxReturn($result);


    }


    public function  updateUser(){
        $id = I('get.id','');
        $model=M('users');

        if(empty($id) ){
            throw new \Think\Exception("id不能为空");
        }
        $user=$model->where("id=".$id)->select();

        if(empty($user)){
            throw new \Think\Exception("未查询到用户");
        }
        $this->assign("user",$user[0]);
        $this->assign("sex","".$user[0]['sex']);
        $this->assign("id","".$id);

        $this->display(':updateUser');
    }


    public function doUpdateUser(){

        try {
            $mail = I('post.mail');
            $deployName = I('post.deployName');
            $sex = I('post.sex');
            $id = I('post.id');
            $map['mail'] = $mail;
            $map['deploy_name'] = $deployName;
            if($sex=='男'){
                $map['sex'] = 1;
            }else if($sex=='女'){
                $map['sex'] = 2;
            }else{
                $map['sex'] = 0;
            }
            $model = M('users');

            $user=$model->where("id=" . $id)->select();
            if(empty($user)){
                throw new \Think\Exception("用户不存在!");
            }

            $model->where("id=" . $id)->save($map);
            $result['status']="success";
            $result['name']=$user[0]['name'];

            $result['message']="修改成功!";
        } catch (\Exception $e) {
            $result['status']="fail";
            $result['message']=$e->getMessage();
        }
        $this->ajaxReturn($result);

    }

    public function doDelUser(){

        try {
            $ids = I('post.ids');
            $model = M('users');
            $map=array();
            $map['id']=array('in',$ids);
            $model->where($map)->delete();

            $result['status']="success";

            $result['message']="删除成功!";
        } catch (\Exception $e) {
            $result['status']="fail";
            $result['message']=$e->getMessage();
        }
        $this->ajaxReturn($result);

    }

}